// 通讯录项目主文件

#include <stdio.h>
#include <string.h>
#include "SeqList.h"
#include "Contact_project.h"

int Contact_Find_Byname(Contact *pcon, char name[])
{
    for (int i = 0; i < pcon->size; i++)
    {
        if (strcmp(pcon->arr[i].name, name) == 0)
        {
            return i;
        }
    }
    return -1;
}

void LoadContact(Contact *pcon)
{
    FILE  *pf = fopen("contact.txt", "rb");
    if (pf == NULL)
    {
        perror("fopen error!\n");
        return;
    }
    // 循环读取文件数据
    Info info;
    while (fread(&info, sizeof(Info), 1, pf))
    {
        SL_Pushback(pcon, info);
    }
    printf("Load successful\n");
}

// 保存到文件
void SaveContact(Contact *pcon)
{
    FILE *pf = fopen("contact.txt", "wb");
    if (pf == NULL)
    {
        perror("fopen error!\n");
        return;
    }
    // 将通讯录数据写入文件
    for (int i = 0; i < pcon->size; i++)
    {
        fwrite(pcon->arr + i, sizeof(Info), 1, pf);
    }
    printf("Save successful\n");
}

/*
 * @brief  通讯录初始化
 * @param  通讯录指针
 * @retval 无
 */
void Contact_Init(Contact *pcon)
{
    SL_Init(pcon);
    LoadContact(pcon);
}

/*
 * @brief  通讯录销毁
 * @param  通讯录指针
 * @retval 无
 */
void Contact_destory(Contact *pcon)
{
    SaveContact(pcon);
    SL_destory(pcon);
}
/* 增加 删除 修改 查找 查看 通讯录 */

/*
 * @brief  添加联系人
 * @param
 * @retval
 */
void Contact_Add(Contact *pcon)
{
    // 创建联系人结构体变量
    Info info;
    printf("Please enter the name:\n");
    scanf("%s", info.name);
    printf("Please enter the age:\n");
    scanf("%d", &info.age);
    printf("Please enter the gender:\n");
    scanf("%s", info.gender);
    printf("Please enter the telephone number:\n");
    scanf("%s", info.tele);
    printf("Please enter the address:\n");
    scanf("%s", info.addr);

    // 保存数据到通讯录
    SL_Pushback(pcon, info);
}

/*
 * @brief  删除联系人
 * @param
 * @retval
 */
void Contact_Delete(Contact *pcon)
{
    // 删除之前进行查找
    // 找到了->删除
    // 找不到->不删除
    printf("please enter the person you want to delete:\n");
    char name[NAME_MAX];
    scanf("%s", name);
    // 通过名字查找并获取下标
    int findindex = Contact_Find_Byname(pcon, name);
    if (findindex < 0)
    {
        printf("the person is not found\n");
        return;
    }
    SLErase(pcon, findindex);
    printf("Delete successful\n");
}

void Contact_Modify(Contact *pcon)
{
    // 修改之前线查找
    // 找到了 修改
    // 没找到 不修改
    char name[NAME_MAX];
    printf("please enter the person you want to modify:\n");
    scanf("%s", name);
    int findindex = Contact_Find_Byname(pcon, name);
    if (findindex < 0)
    {
        printf("the person is not found\n");
        return;
    }
    printf("please enter the new name:\n");
    scanf("%s", pcon->arr[findindex].name);
    printf("please enter the new gender:\n");
    scanf("%s", pcon->arr[findindex].gender);
    printf("please enter the new age:\n");
    scanf("%d", &pcon->arr[findindex].age);
    printf("please enter the new telephone number:\n");
    scanf("%s", pcon->arr[findindex].tele);
    printf("please enter the new address:\n");
    scanf("%s", pcon->arr[findindex].addr);
    printf("OK\n");
}

void Contact_Find(Contact *pcon)
{
    char name[NAME_MAX];
    printf("please enter the person you want to find:\n");
    scanf("%s", name);
    int findindex = Contact_Find_Byname(pcon, name);
    if (findindex < 0)
    {
        printf("the person is not found\n");
        return;
    }
    printf("-----------------------------------------------\n");
    printf("%-10s%-10s%-10s%-12s%-10s\n", "name", "gender", "age", "tele", "add");
    printf("%-10s%-10s%-10d%-12s%-10s\n",
           pcon->arr[findindex].name,
           pcon->arr[findindex].gender,
           pcon->arr[findindex].age,
           pcon->arr[findindex].tele,
           pcon->arr[findindex].addr);
    printf("-----------------------------------------------\n");
}

/*
 * @brief  打印通讯录
 * @param
 * @retval
 */
void Contact_Show(Contact *pcon)
{
    printf("-----------------------------------------------\n");
    printf("%-10s%-10s%-10s%-12s%-10s\n", "name", "gender", "age", "tele", "add");
    for (int i = 0; i < pcon->size; i++)
    {
        printf("%-10s%-10s%-10d%-12s%-10s\n",
               pcon->arr[i].name,
               pcon->arr[i].gender,
               pcon->arr[i].age,
               pcon->arr[i].tele,
               pcon->arr[i].addr);
    }
    printf("-----------------------------------------------\n");
}
